package cn.lmxdev.ptms.service.impl;

import cn.lmxdev.ptms.bean.exception.BizException;
import cn.lmxdev.ptms.bean.table.TSpecialtyStudent;
import cn.lmxdev.ptms.dao.ISpecialtyStudentDao;
import cn.lmxdev.ptms.service.ISpecialtyStudentServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpecialtyStudentServletImpl implements ISpecialtyStudentServlet {
    private final ISpecialtyStudentDao specialtyStudentDao;

    @Autowired
    public SpecialtyStudentServletImpl(
            ISpecialtyStudentDao specialtyStudentDao
    ) {
        this.specialtyStudentDao = specialtyStudentDao;
    }

    @Override
    @Transactional
    public TSpecialtyStudent get(Long id) {
        return this.specialtyStudentDao.find(id);
    }

    @Override
    @Transactional
    public long add(TSpecialtyStudent specialtyStudent) {
        // 插入数据
        if (!this.specialtyStudentDao.insert(specialtyStudent))
            throw new BizException("操作失败");

        // 返回ID
        return specialtyStudent.getId();
    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 删除数据
        if (!this.specialtyStudentDao.delete(id))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public void edit(TSpecialtyStudent specialtyStudent) {
        // 查询旧数据
        TSpecialtyStudent oldSpecialtyStudent = this.specialtyStudentDao.find(specialtyStudent.getId());

        // 更新属性
        oldSpecialtyStudent.setSpecialtyId(specialtyStudent.getSpecialtyId());

        // 保存数据
        if (!this.specialtyStudentDao.update(oldSpecialtyStudent))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public long count(TSpecialtyStudent specialtyStudent) {
        return this.specialtyStudentDao.count(specialtyStudent);
    }

    @Override
    @Transactional
    public List<TSpecialtyStudent> list(Long limit, Long page, TSpecialtyStudent specialtyStudent) {
        return this.specialtyStudentDao.select(page != null ? (page - 1) * limit : null, limit, specialtyStudent);
    }
}
